﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DemoUser.Application.User.Dtos;
using DemoUser.Application.User.Interface;
using DemoUser.Core.User;

using SqlSugar;

using StackExchange.Profiling.Internal;

namespace DemoUser.Application.User.Services
{
    public class TestUserService:ITestService,ITransient
    {
        private readonly ISqlSugarClient _client;

        public TestUserService(ISqlSugarClient client)
        {
            _client = client;
        }

        public int DelById(int id)
        {
            return _client.Deleteable<TbUser>().In(id).ExecuteCommand();
        }

        public int DelByIds(int[] ids)
        {
            return _client.Deleteable<TbUser>()
                .In(user => user.Id,ids)
                .ExecuteCommand();
        }

        public void InsertOne(TbUser tbUser)
        {
            _client.Insertable(tbUser).ExecuteCommand();
        }

        public bool IsExist(TbUser user)
        {
            return _client.Queryable<TbUser>().Any(user => user.Id == user.Id);
        }

        public List<TbUser> SelectAll()
        {
            return _client.Queryable<TbUser>()
                .ToList();
        }

        public TbUser SelectById(int id)
        {
            return _client.Queryable<TbUser>().Single(x => x.Id == id);
        }

        public List<TbUser> SelectByPage(int pageNum, int pageSize)
        {
            int pageTotal = 0;
            var page = _client.Queryable<TbUser>()
                .ToPageList(pageNum, pageSize, ref pageTotal);
            Console.WriteLine(pageTotal.ToString());
            return page;
        }

        public List<TbUser> SelectByUsername(string username)
        {
            return _client.Queryable<TbUser>()
                .Where(user => user.Username == username)
                .ToList();
        }

        public List<TbUser> SelectByUsernameAndPassword(string username, string password)
        {
            return _client.Queryable<TbUser>()
                .Where(user => user.Username == username && user.Password == password)
                .ToList();
        }

        public int SelectCount()
        {
            return _client.Queryable<TbUser>()
                .Count();
        }

        public List<TbUser> SelectCount(int count)
        {
            return _client.Queryable<TbUser>()
                .Take(count)
                .ToList();
        }

        public TbUser SelectFirst()
        {
            return _client.Queryable<TbUser>()
                .First();
        }

        public List<TbUserDto> SelectGroupById()
        {

            TbUserDto t = _client.Queryable<TbUser>().ToList()
                .Where(ut => ut.Id > 0)
                .GroupBy(ut => ut.Id)
                .Select(ut => new TbUserDto
                {
                    Id = ut.Key,
                    tbUsers = ut.ToList()
                }).First();
            Console.WriteLine(t.ToJson());

            return _client.Queryable<TbUser>().ToList()
                .Where(ut => ut.Id > 0)
                .GroupBy(ut => ut.Id)
                .Select(ut => new TbUserDto
                {
                    Id = ut.Key,
                    tbUsers = ut.ToList()
                }).ToList();
        }

        public List<TbUser> SelectInUsername(string[] username)
        {
            return _client.Queryable<TbUser>()
                .Where(user => username.Contains(user.Username))
                .ToList();
        }

        public List<TbUser> SelectInUsernameAndPassword(TbUser[] tbUsers)
        {
            return _client.Queryable<TbUser>()
                .Where(user => tbUsers.Any(u => u.Username == user.Username && u.Password == user.Password))
                .ToList();
        }

        public TbUser SelectLast()
        {
            return _client.Queryable<TbUser>()
                .OrderBy(user => user.Id,OrderByType.Desc)
                .First();
        }

        public List<TbUser> SelectLikeInUsername(string[] usernames)
        {
            return _client.Queryable<TbUser>()
                .Where(user => usernames.Any(u => user.Username.Contains(u)))
                .ToList();
        }

        public List<TbUser> SelectLikeInUsernameAndPassword(TbUser[] tbUsers)
        {
            return _client.Queryable<TbUser>()
                .Where (user => tbUsers.Any(u => user.Username.Contains(u.Username)))
                .ToList();
        }

        public List<TbUser> SelectLikeUsername(string username)
        {

            return _client.Queryable<TbUser>()
                .Where(user => user.Username.Contains(username))
                .ToList();
        }

        public List<TbUser> SelectNotInUsernames(string[] username)
        {
            return _client.Queryable<TbUser>()
                .Where(user => !username.Contains(user.Username))
                .ToList();
        }

        public List<TbUser> SelectNotPassword()
        {
            return _client.Queryable<TbUser>()
                .IgnoreColumns(user => user.Password)
                .ToList();
        }

        public int UpdatePassword()
        {
            return _client.Updateable<TbUser>()
                .Where(user => user.Password.Equals("123"))
                .SetColumns(user => user.Password == "123456")
                .ExecuteCommand();
        }

        List<TbUser> ITestService.OrderAfterSelect()
        {
            throw new NotImplementedException();
        }

        List<TbUser> ITestService.OrderBeforeSelect()
        {
            throw new NotImplementedException();
        }
    }
}
